import express from "express";
import router from "./routes";
import sequelize from "./db";
import { appConfig, validateConfig, printConfig } from "./config";

const app = express();

// 验证配置
if (!validateConfig()) {
  console.error("配置验证失败，程序退出");
  process.exit(1);
}

// 打印配置信息
printConfig();

// 全局中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 路由注册
app.use("/", router);

// 启动服务并连接数据库
async function startServer() {
  try {
    await sequelize.authenticate();
    console.log("数据库连接成功");
    await sequelize.sync({ force: false });
    console.log("数据库模型同步完成");
    app.listen(appConfig.port, () => {
      console.log(`服务器运行在 http://localhost:${appConfig.port}`);
    });
  } catch (error) {
    console.error("启动失败:", error);
  }
}

startServer();
